package mutilcore;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.RecursiveTask;

public class QuickSortTask extends RecursiveTask<Integer> {

    private static final long serialVersionUID = 1L;
    private static final int THRESHOLD = 1000;
    private List<Integer> nums;

    public QuickSortTask(List<Integer> nums) {
        this.nums = nums;
    }

    @Override
    protected Integer compute() {
        int sum = 0;
        if (nums.size() < THRESHOLD) {
            Collections.sort(nums);
        } else {
            int middle = nums.size() / 2;
            QuickSortTask left = new QuickSortTask(nums.subList(0, middle));
            QuickSortTask right = new QuickSortTask(nums.subList(middle,
                    nums.size()));
            left.fork();
            right.fork();
            middle = left.join() + right.join();
        }
        return sum;
    }

}